package com.hejia.bulbul.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.hejia.alauda.entity.BankInfo;
import com.hejia.alauda.entity.PayGateBank;
import com.hejia.alauda.service.PaymentService;
import com.hejia.alauda.utils.HttpResponse;

@Controller
@RequestMapping("paygatebank")
public class PayGateBankController extends BaseController {

	@Resource
	private PaymentService payGateBankService;

	@Resource
	private PaymentService bankInfoService;

	@RequestMapping(value = "", method = RequestMethod.POST)
	@ResponseBody
	public HttpResponse add(PayGateBank payGateBank, String bankCode) {
		HttpResponse response = new HttpResponse();
		try {
			BankInfo bankInfo = bankInfoService.getBankInfoByBankCode(bankCode);
			payGateBank.setCreateDate(new Date());
			payGateBank.setBankCode(bankCode);
			payGateBank.setBankName(bankInfo.getBankName());
			if (payGateBank.getWebEnable() == null) {
				payGateBank.setWebEnable(false);
			}
			if (payGateBank.getH5Enable() == null) {
				payGateBank.setH5Enable(false);
			}
			if (payGateBank.getiOSEnable() == null) {
				payGateBank.setiOSEnable(false);
			}
			if (payGateBank.getAndroidEnable() == null) {
				payGateBank.setAndroidEnable(false);
			}
			payGateBankService.addPayGateBank(payGateBank);
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("id", payGateBank.getId());
			map.put("bankName", payGateBank.getBankName());
			map.put("payBankCode", payGateBank.getPayBankCode());
			map.put("singleLimit", payGateBank.getSingleLimit());
			map.put("dayLimit", payGateBank.getDayLimit());
			if (payGateBank.getMonthLimit() == 0) {
				map.put("monthLimit", "无限制");
			} else {
				map.put("monthLimit", payGateBank.getMonthLimit());
			}
			map.put("webEnable", payGateBank.getWebEnable());
			map.put("h5Enable", payGateBank.getH5Enable());
			map.put("iOSEnable", payGateBank.getiOSEnable());
			map.put("androidEnable", payGateBank.getAndroidEnable());
			map.put("inuse", payGateBank.getInuse());

			response.setCode("201");
			response.setStatus("success");
			response.setMessage("添加银行成功");
			response.setData(map);
		} catch (Exception e) {
			response.setCode("400");
			response.setStatus("error");
			response.setMessage("添加银行失败");
			response.setData("");
		}

		return response;
	}

	@RequestMapping(value = "delete/{id}", method = RequestMethod.POST)
	@ResponseBody
	public HttpResponse delete(@PathVariable long id) {
		HttpResponse response = new HttpResponse();
		try {
			PayGateBank payGateBank = new PayGateBank();
			payGateBank.setId(id);
			this.payGateBankService.deletePayGateBank(payGateBank);
			response.setCode("201");
			response.setStatus("success");
			response.setMessage("删除银行成功");
			response.setData(id);
		} catch (Exception e) {
			response.setCode("400");
			response.setStatus("error");
			response.setMessage("删除银行失败");
			response.setData("");
		}

		return response;
	}

	@RequestMapping(value = "showupdate/{id}", method = RequestMethod.GET)
	public String showupdate(@PathVariable long id, ModelMap model) {
		PayGateBank payGateBank = payGateBankService.findPayGateBankById(id);
		List<BankInfo> banklist = bankInfoService.findAllBankInfo();
		model.put("banklist", banklist);
		model.put("pgBank", payGateBank);
		return "/setting/bankupdate";
	}

	@RequestMapping(value = "update/{id}", method = RequestMethod.POST)
	@ResponseBody
	public HttpResponse update(PayGateBank payGateBank, @PathVariable long id,
			String bankCode) {
		HttpResponse response = new HttpResponse();
		try {
			PayGateBank payGateBank2 = payGateBankService
					.findPayGateBankById(id);
			BankInfo bankInfo = bankInfoService.getBankInfoByBankCode(bankCode);
			payGateBank2.setBankName(bankInfo.getBankName());
			payGateBank2.setBankCode(bankCode);
			payGateBank2.setPayBankCode(payGateBank.getPayBankCode());
			payGateBank2.setSingleLimit(payGateBank.getSingleLimit());
			payGateBank2.setDayLimit(payGateBank.getDayLimit());
			payGateBank2.setMonthLimit(payGateBank.getMonthLimit());
			payGateBank2.setWebEnable(payGateBank.getWebEnable()!=null?payGateBank.getWebEnable():false);
			payGateBank2.setH5Enable(payGateBank.getH5Enable()!=null?payGateBank.getH5Enable():false);
			payGateBank2.setiOSEnable(payGateBank.getiOSEnable()!=null?payGateBank.getiOSEnable():false);
			payGateBank2.setAndroidEnable(payGateBank.getAndroidEnable()!=null?payGateBank.getAndroidEnable():false);
			payGateBank2.setInuse(payGateBank.getInuse());
			payGateBank2.setModifyDate(new Date());
			payGateBankService.modifyPayGateBank(payGateBank2);

			Map<String, Object> map = new HashMap<String, Object>();
			map.put("id", payGateBank2.getId());
			map.put("bankName", payGateBank2.getBankName());
			map.put("payBankCode", payGateBank2.getPayBankCode());
			map.put("singleLimit", payGateBank2.getSingleLimit());
			map.put("dayLimit", payGateBank2.getDayLimit());
			if (payGateBank2.getMonthLimit() == 0) {
				map.put("monthLimit", "无限制");
			} else {
				map.put("monthLimit", payGateBank.getMonthLimit());
			}
			map.put("webEnable", payGateBank2.getWebEnable());
			map.put("h5Enable", payGateBank2.getH5Enable());
			map.put("iOSEnable", payGateBank2.getiOSEnable());
			map.put("androidEnable", payGateBank2.getAndroidEnable());
			map.put("inuse", payGateBank2.getInuse());
			response.setCode("201");
			response.setStatus("success");
			response.setMessage("修改银行成功");
			response.setData(map);
		} catch (Exception e) {
			response.setCode("400");
			response.setStatus("error");
			response.setMessage("修改银行失败");
			response.setData("");
		}

		return response;
	}

}
